var myText=decodeURIComponent(location.search)
var getCookie=readCookie("user")
var pack1=""//商品包装
var num1=""//记录购买商品数量
var num=1
var myIsCid=""
$(function(){
	myVue()
		
	initData()
	selectListCommodity()
	packData()
	getGoodComment()
	var getCount=getComment()
	$("#commentCount").html(getCount)
	$("#goodComment").html(getCount)
	myPage()//加载分页

	
})
	


function myPage(){
	
	var page = new Pagin({
		el:"#page",	//绑定分页div
		dataSize:getComment(),	//数据量
		pageSize:5	,//每页数量
		func:function(){
			num=PaginJson.pageIndex
			getComment()
		}
	})
}
function myVue(){
	vm=new Vue({
		el:".Inside_pages",
		data:{
			info:[],
			pinfo:[],
			cinfo:[],
			llist:[]
			
		},
		methods:{
			getPack(pack){
				setDetail(pack)
			},
			countNum(id){
				countNum(id)
			},
			addCart(id){
				addCart(id)
			},
			buyCommodity(id){
			buyCommodity (id)
			},
			zCommodity(id){
				zCommodity(id)
				}
		}
		
	})
	
}
function zCommodity(id){
	myText="?id="+id
	initData()
	}
function buyCommodity (id){
	var pack=vm.$data.pinfo
	var num1=$("#buy-num").val()
	//获取商品包装
	for(var i=0;i<parseInt(pack.length);i++){
	
		if($("#a"+pack[i].p_id).css("borderLeftColor")=="rgb(204, 51, 0)"){
			pack1=pack[i].p_name
		}
	}
	if(pack1==""){
		
		$.DialogByZ.Alert({Title: "提示", Content: "请选择商品",BtnL:"确定"})
		
	}else{
		location.href="Orders.html?cNum="+num1+"&id="+id
	}
}
function addCart(id){
	//如果没有登陆就跳转到登陆页面
	if(getCookie==""){
		location.href="login.htm"
	}

	//商品类型
	var cType=""
	//商品包装类型
	var cSpecs=""
	//商品名称
	var cName=""
	//包装名称临时变量
	var p_name=""
	//商品展示图片id
	var cImg=""
	//商品单价
	var price=""
	//商品总价
	var sPrice=""
	//获取购买数量
	var stoke=$("#buy-num").val()
	var pack=vm.$data.pinfo
	var getCommodity=vm.$data.info
	for(var i=0;i<getCommodity.length;i++){
		if(getCommodity[i].id==id){
			cImg=getCommodity[i].img.tImg
			price=getCommodity[i].activity_prices
			sPrice=parseFloat(price)*parseInt(stoke)
			cName=getCommodity[i].name
			cSpecs=getCommodity[i].cPack.p_name
			cType=getCommodity[i].type.tName
		}
	}
	//获取商品包装
	for(var i=0;i<parseInt(pack.length);i++){

		if($("#a"+pack[i].p_id).css("borderLeftColor")=="rgb(204, 51, 0)"){
			p_name=pack[i].p_name
		}
	}
	
	if(p_name==""){
		$.DialogByZ.Alert({Title: "提示", Content: "请选择包装方式",BtnL:"确定"})
		
	}else{
		if(getNum(id)==1){
				$.ajax({
				url:"http://localhost:8080/shopcar/add",
				type:"post",
				async:false,
				data:{
					"uId":getCookie,
					"cId":id,
					"cImg":cImg,
					"cPrice":price,
					"sPrice":sPrice,
					"cNum":stoke,
					"cName":cName,
					"cType":cType,
					"cSpecs":cSpecs
				},
				dataType:"json",
				success:function(data){
				if(data.status==1){
					getUcount()
					$.DialogByZ.Autofade({Content: "添加购物车成功"})
				}else{
					$.DialogByZ.Alert({Title: "提示", Content: "服务器异常",BtnL:"确定"})
				}
			
				}
			})
		}else{
				$.ajax({
				url:"http://localhost:8080/shopcar/addNum",
				type:"get",
				async:false,
				data:{
					"cId":id,
					"uId":getCookie,
					"cNum":stoke,
					"sPrice":sPrice
					},
				dataType:"json",
				success:function(data){
					$.DialogByZ.Autofade({Content: data.msg})
				}
			})
		}
		
	}
	
}
function getNum(id){
	var num=""
		$.ajax({
		url:"http://localhost:8080/shopcar/getNum",
		type:"get",
		async:false,
		data:{
			"cId":id,
			"uId":getCookie
			},
		dataType:"json",
		success:function(data){
			num=data.status
	
		}
	})
	return num
}
function countNum(num){//获取详情页商品数量
	var myNum=$("#buy-num").val()
	var stock=$("#stoke").val()
	//alert(stock+":"+myNum)
	if(!myNum){
		$("#buy-num").val(1)
	}
	 myNum=$("#buy-num").val()
	if(num==0&&myNum>1){
		$("#buy-num").val(parseInt(myNum)-1)
	}else if(num==1&&parseInt(myNum)<parseInt(stock)){
		
		$("#buy-num").val(parseInt(myNum)+1)
	}
}
function setDetail(specs){
		
		$.ajax({
		url:"http://localhost:8080/commodity/chooseCommodity"+myText+"&specs="+specs,
		type:"get",
		async:false,
		dataType:"json",
		success:function(data){
			if(data.data.length>0){
			packData()
			$("#midimg").prop("src","http://localhost:8080/"+data.data[0].img.tImg+"")
			vm.$data.info=data.data	
			var getCount2=getComment()
			$("#commentCount").html(getCount2)
			$("#goodComment").html(getCount2)
			getDetails(data.data[0].id)
			selectByCid(data.data[0].id)
			$("#myName").html(data.data[0].name)
			$("#mySpes").html(data.data[0].weight+"g")
			$("#myPack").html(data.data[0].cPack.p_name)
			$("#myBrand").html(data.data[0].cBrand.b_name)
			$("#myPrice1").html(data.data[0].activity_prices+"元")
			num=1
			
			myPage()
			$("#a"+specs).css("border","2px solid #C30")
			}else{
				
				$.DialogByZ.Autofade({Content: "此包装暂无"})
			}
			
	
		}
	})
}
function initData(){
		$.ajax({
		url:"http://localhost:8080/commodity/chooseCommodity"+myText,
		type:"get",
		async:false,
		dataType:"json",
		success:function(data){
			vm.$data.info=data.data
			myIsCid=data.data[0].id
			getDetails(myIsCid)
			selectByCid(myIsCid)
			getGoodComment()
			myPage()
			var getCount2=getComment()
			$("#commentCount").html(getCount2)
			$("#goodComment").html(getCount2)
			$("#midimg").prop("src","http://localhost:8080/"+data.data[0].img.tImg+"")
			$("#myName").html(data.data[0].name)
			$("#mySpes").html(data.data[0].weight+"g")
			$("#myPack").html(data.data[0].cPack.p_name)
			$("#myBrand").html(data.data[0].cBrand.b_name)
			$("#myPrice1").html(data.data[0].activity_prices+"元")
		}
	})
}


function packData(){
		$.ajax({
		url:"http://localhost:8080/pack/select",
		type:"get",
		async:false,
		dataType:"json",
		success:function(data){
			for(var i=0;i<data.data.length;i++){
				$("#a"+data.data[i].p_id).css("border","1px solid #C0C0C0")
			}
			
			vm.$data.pinfo=data.data
		}
	})
}
function getComment(){
	var isCount=0
	
	$.ajax({
		url:"http://localhost:8080/comment/selectByCid",
		type:"get",
		async:false,
		data:{
			"pageNum":num,
			"pageSize":"5",
			"cId":myIsCid
		},
		dataType:"json",
		success:function(data){
			isCount=data.data.total
			vm.$data.cinfo=data.data.list
			if(isCount==0){
				$(".pj_two").hide()
				}else if(isCount>0){
					$(".pj_two").show()
				}
			
			
		}
	})
	
	return isCount
}
function getDetails(c_id){
	$.ajax({
		url:"http://localhost:8080/detail/select",
		type:"get",
		data:{
			"c_id":c_id
		},
		async:false,
		dataType:"json",
		success:function(data){
			$(".myDetails1").html("")
			//alert("<img src="+data.data[0].img+">")
			for(var i=0;i<data.data.length;i++){
				
				$(".myDetails1").append("<img width='949px' src=http://localhost:8080/"+data.data[i].img+">")
			}
			
			
		}
	})
}
function selectByCid(cId){
		
	 
	$.ajax({
		url:"http://localhost:8080/collect/selectBycId",
		type:"get",
		dataType:"json",
		async:false,
		data:{
			"cId":cId
		},
		success:function(data){
			
		$(".Collect").html("<em class='ico1'></em>被收藏总量("+data.data+" )")	
		
			
			
		
		}
	})
	
}

function selectListCommodity(){
		
	 
	$.ajax({
		url:"http://localhost:8080/commodity/chooseCommodity",
		type:"get",
		dataType:"json",
		async:false,
		success:function(data){
			
			
		vm.$data.llist=data.data
			
			
		
		}
	})
	
}

function getGoodComment(){
	var isCount=0
	
	$.ajax({
		url:"http://localhost:8080/comment/selectByCid",
		type:"get",
		async:false,
		data:{
			"cId":myIsCid
		},
		dataType:"json",
		success:function(data){
			var getSun=data.data.list.length
			var j=0;
			var c=0;
			var x=0;
			for(var i=0;i<data.data.list.length;i++){
				if(data.data.list[i].cStar>=4){
					j++;
				}else if(data.data.list[i].cStar<4&&data.data.list[i].cStar>=2){
					c++;
				}else if(data.data.list[i].cStar==1){
					x++;
				}
			}
			if(getSun>0){
				$(".myGoodComment").html((j/getSun)*100+"%")
				$("#cComment").html((c/getSun)*100+"%")
				$("#xComment").html((x/getSun)*100+"%")
				$("#goodRate").html((j/getSun)*100+"<span>%</span>")
			}else{
				$(".myGoodComment").html("0%")
				$("#cComment").html("0%")
				$("#xComment").html("0%")
				$("#goodRate").html("0<span>%</span>")
			}
			
			
		}
	})
	
	return isCount
}
